package it.asvp.asvp_portal.data.dao.impl;

import it.asvp.asvp_portal.data.dao.MemberAccountUpdateDao;
import it.asvp.asvp_portal.data.entity.member.Member;
import it.asvp.asvp_portal.data.entity.member.MemberAccountUpdate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class MemberAccountUpdateDaoImpl implements MemberAccountUpdateDao {

  private static final Log log = LogFactory.getLog(MemberAccountUpdateDaoImpl.class);

  @Autowired
  SessionFactory           sessionFactory;

  @Override
  public Long persist(MemberAccountUpdate transientInstance) {
    log.debug("persisting MemberAccountUpdate instance");
    Long id;
    try {
      id = (Long) sessionFactory.getCurrentSession().save(transientInstance);
      log.debug("persist successful");
    } catch (RuntimeException re) {
      log.error("persist failed", re);
      throw re;
    }
    return id;
  }

  @Override
  public void delete(MemberAccountUpdate persistentInstance) {
    log.debug("deleting MemberAccountUpdate instance");
    try {
      sessionFactory.getCurrentSession().delete(persistentInstance);
      log.debug("delete successful");
    } catch (RuntimeException re) {
      log.error("delete failed", re);
      throw re;
    }
  }

  @Override
  public MemberAccountUpdate merge(MemberAccountUpdate detachedInstance) {
    log.debug("merging MemberAccountUpdate instance");
    try {
      MemberAccountUpdate result = (MemberAccountUpdate) sessionFactory.getCurrentSession().merge(detachedInstance);
      log.debug("merge successful");
      return result;
    } catch (RuntimeException re) {
      log.error("merge failed", re);
      throw re;
    }
  }

  @Override
  public MemberAccountUpdate findById(java.lang.Long id) {

    Criteria cr = sessionFactory.getCurrentSession().createCriteria(MemberAccountUpdate.class);
    cr.add(Restrictions.eq("id", id));
    MemberAccountUpdate mAU = (MemberAccountUpdate) cr.uniqueResult();
    return mAU;
  }

  @Override
  @SuppressWarnings("unchecked")
  public List<MemberAccountUpdate> findUpdatedByYear(Member councilor, Integer updateYear) {

    Criteria cr = sessionFactory.getCurrentSession().createCriteria(MemberAccountUpdate.class);
    cr.add(Restrictions.eq("councilor", councilor));
    cr.add(Restrictions.eq("updateYear", updateYear));
    List<MemberAccountUpdate> list = (List<MemberAccountUpdate>) cr.list();
    return list;
  }

  @Override
  public MemberAccountUpdate findByYear(Member member, Integer updateYear) {

    Criteria cr = sessionFactory.getCurrentSession().createCriteria(MemberAccountUpdate.class);
    cr.add(Restrictions.eq("member", member));
    cr.add(Restrictions.eq("updateYear", updateYear));
    MemberAccountUpdate memberAccountUpdate = (MemberAccountUpdate) cr.uniqueResult();
    return memberAccountUpdate;
  }
}
